package com.aabte.design2.structural.wrapper;

/**
 * 客户端使用外部数据源。SalaryManager（工资管理器）对象并不关心
 * 数据如何存储。它们会与提前配置好的数据源进行交互，数据源则是通过程序配
 * 置器获取的。
 *
 * @author Daniel
 * @version 1.0
 * @date 2020/5/3
 */
public class SalaryManager {

    private DataSource dataSource;

    /**
     * 程序可在运行时根据配置或环境组装不同的装饰堆桟
     */
    public static void main(String[] args) {

        if (null == args || args.length == 0) {
            args = new String[]{"true", "true"};
        }

        DataSource dataSource = new FileDataSource();

        if (args.length >= 2) {
            boolean enabledEncryption = "true".equalsIgnoreCase(args[0]);
            if (enabledEncryption) {
                dataSource = new EncryptionDecorator(dataSource);
            }
            boolean enabledCompression = "true".equalsIgnoreCase(args[1]);
            if (enabledCompression) {
                dataSource = new CompressionDecorator(dataSource);
            }
        }

        SalaryManager salaryManager = new SalaryManager(dataSource);
        salaryManager.save("salary_data");
        System.out.println("--------------------");
        String data = salaryManager.load();
        System.out.println("data = " + data);
    }

    public SalaryManager(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public String load() {
        return dataSource.readData();
    }

    public void save(String data) {
        dataSource.writeData(data);
    }
}
